//mock.js 文件
import Mock from 'mockjs' // 引入mockjs
const Random = Mock.Random // Mock.Random 是一个工具类，用于生成各种随机数据

const dataList = [] // 用于接受生成数据的数组
for (let i = 0; i < 100; i++) {
  // 可自定义生成的个数
  const template = {
    date: Random.date(), // 生成一个随机日期,可加参数定义日期格式
    name: Random.name(), // 生成姓名
    address: Random.province(), // 生成地址
  }
  dataList.push(template)
}
// list 分页接口()

Mock.mock('/api/list', 'post', (params) => {
  let info = JSON.parse(params.body)
  let [index, size, total] = [info.current, info.pageSize, dataList.length]
  let len = total / size
  let totalPages =
    len - parseInt(String(len)) > 0 ? parseInt(String(len)) + 1 : len
  let newDataList = dataList.slice(index * size, (index + 1) * size)
  return {
    code: '200',
    message: '获取成功',
    data: {
      current: index,
      pageSize: size,
      rows: newDataList,
      total: total,
      totalPages: totalPages,
    },
  }
})
Mock.mock('/api/list', 'post', (params) => {
  let info = JSON.parse(params.body)
  let [index, size, total] = [info.current, info.pageSize, dataList.length]
  let len = total / size
  let totalPages =
    len - parseInt(String(len)) > 0 ? parseInt(String(len)) + 1 : len
  let newDataList = dataList.slice(index * size, (index + 1) * size)
  return {
    code: '200',
    message: '获取成功',
    data: {
      current: index,
      pageSize: size,
      rows: newDataList,
      total: total,
      totalPages: totalPages,
    },
  }
})

// 随机经纬度
Mock.mock('/api/location', 'get', () => {
  // 定义经纬度范围
  const NE = { latitude: 23.169457, longitude: 113.436898 } // 东北角
  const SW = { latitude: 23.161822, longitude: 113.42456 } // 西南角

  // 函数：生成范围内的随机数
  const getRandomCoordinate = (id, minLat, maxLat, minLng, maxLng) => {
    return {
      id: id, // 使用下标作为 id
      latitude: Mock.Random.float(minLat, maxLat, 6, 6), // 生成6位小数的随机纬度
      longitude: Mock.Random.float(minLng, maxLng, 6, 6), // 生成6位小数的随机经度
    }
  }

  // 生成10个经纬度数据项，并添加递增的 id
  const coordinates = Array.from({ length: 10 }, (_, index) =>
    getRandomCoordinate(
      index + 1,
      SW.latitude,
      NE.latitude,
      SW.longitude,
      NE.longitude,
    ),
  )

  return {
    code: '200',
    message: '获取成功',
    data: coordinates,
  }
})
